<?php


namespace app\admin\model;

use app\admin\model\SysModel;
use app\mini\model\CustomerPointModel;
use think\facade\Db;
use app\admin\model\Common;

class GoodsMini extends Common
{
    protected $name = 'shop_goods_mini';
    
    
    // 列表
    public function miniGoodsList(array $param)
    {
        $page = $param['page'] ?? 1;
        $pagesize = $param['pagesize'] ?? 10;
        $where = [];
        // 商品名称
        if(!empty($param['name']))
        {
            $where[] = ['name','like','%'.$param['name'].'%'];
            
        }
        // 编码
        if(!empty($param['kingdee_code']))
        {
            $where[] = ['kingdee_code','=',$param['kingdee_code']];
        }
        // 状态
        if(!empty($param['status']))
        {
            $where[] = ['status','=',$param['status']];
        }
        
        $data = [];
        $data = Db::name('shop_goods_mini')
            ->whereNull('delete_time')
            ->where($where)
            ->where('delete_time',)
            ->paginate([
            'list_rows'=>$pagesize,
            'page'=>$page,
            'query'=>request()->param()
        ]);
        return $data;
    }
    
    // 详情
    public function miniGoodsInfo(array $param)
    {
        $info = [];
        $info = Db::name('shop_goods_mini')
            ->whereNull('delete_time')
            ->where('id',$param['id'])
            ->where('delete_time',)
            ->find();
        return $info;
    }
    
    
    
    // 添加
    public function miniGoodsCreate(array $request)
    {
        // 定义必填字段及其中文名称
        $requiredFields = [
            'goods_id' => '关联商品ID',
            'name' => '商品名称',
            'mini_unit' => '商品最小单位',
        ];
        foreach ($requiredFields as $field => $name)
        {
            if (!array_key_exists($field, $request))
            {
                $this->error = "{$name}不能为空";
                return false;
            }
        }
        
        try {
            
            // 检查商品名称是否重复 - 使用ThinkPHP兼容的方法
            $nameCount = $this->where('name', $request['name'])->count();
            if ($nameCount > 0)
            {
                $this->error = '商品名称已存在';
                return false;
            }
            $request['create_time'] = time();
            // 执行数据插入
            $miniID = $this->insertGetId($request);
            
            // 检查插入结果
            if (!$miniID)
            {
                $this->error = '添加失败';
                return false;
            }
            return $miniID;
        } catch (\Exception $e)
        {
            // 捕获数据库异常
            $this->error = '添加失败: ' . $e->getMessage();
            return false;
        }
    }
    
    
    
    // 修改
    public function miniGoodsUpdate(array $request)
    {
        // 验证必填字段
        $requiredFields = [
            'id' => '记录ID',
            'goods_id' => '关联商品ID',
            'name' => '商品名称',
            'mini_unit' => '商品最小单位',
        ];
        
        foreach ($requiredFields as $field => $name)
        {
            if (!array_key_exists($field, $request))
            {
                $this->error = "{$name}不能为空";
                return false;
            }
        }
        
        // 获取当前记录ID
        $id = $request['id'];
        
        try {
            // 检查记录是否存在
            $record = $this->find($id);
            if (!$record)
            {
                $this->error = '记录不存在';
                return false;
            }
            
            // 检查商品名称是否重复（排除自身）
            $nameCount = $this->where('name', $request['name'])->where('id', '<>', $id)->count();
            if ($nameCount > 0)
            {
                $this->error = '商品名称已存在';
                return false;
            }
            
            // 检查金蝶编码是否重复（非空时才检查，排除自身）
            if (!empty($request['kingdee_code']))
            {
                $codeCount = $this->where('kingdee_code', $request['kingdee_code'])->where('id', '<>', $id)->count();
                if ($codeCount > 0)
                {
                    $this->error = '金蝶编码已存在';
                    return false;
                }
            }
            
            $request['update_time'] = time();
            // 更新数据
            $result = $this->where('id', $id)->update($request);
            
            if ($result === false)
            {
                $this->error = '更新失败';
                return false;
            }
            
            return true;
        } catch (\Exception $e) {
            $this->error = '更新失败: ' . $e->getMessage();
            return false;
        }
    }
    
    
    // 删除
    public function miniGoodsDelete(array $request)
    {
        $check = $this->where('id', $request['id'])->count();
        if($check > 0)
        {
            $result = $this->where('id', $request['id'])->update(['delete_time' => time()]);
            return true;
        }else
        {
            $this->error = '未找到该商品信息';
            return false;
        }
    }
    
    
    
    
    
}